package com.cfk.shopmail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.cfk.shopmail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118694229";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCsLBJhGdoihHH2w/8WmvrZj/PFLTb4nGkrmqeXXu9IuvLTI/y5h65/xE2zZttjjnWp+L8FiBVBkYP5VZwTvrl/Fd3BKhlTd5byAiMbzRsNa6xNPS29cQZsQ93JXm7RClG89ZUcD61zXEld0kA1YMZ658h+GYvt6yamV0X/dpoFFji7KCsdGAZKfPIJ0KDZBvvrlV7Ss+j/w3mPz5CWQ9DCExKDmaAPXiVWhQ9D8MzYOrHDDSigx/19jibccL6UyeteTmlf51vl1pI+Rs8IOzxZj9f42Pr+7INbwfh1jOhQ3d+lGFkYv/sFiAIp1aXK3wLj0r9Z6Sa53eMXl99zVJvZAgMBAAECggEAIqFiuQE4LQUy3w2hO2zE1AnuYFSt3+emB809tGa1/o0L1ohAhxjRcoWLk3A25Jl89UlBMt2iWOPqBbbAhAAy02sUhYvwOCevxtHDOg5RSKvFpmuNzbL0NcaYz64c4BMRSc6jxVcp7jA72ZE2tQXYJlSEsUh2Ks7HoWGghLfYVvlCLc07IEkBqWAuZkQNpPQHMNtsngrGn18LmTyzBAzwbQuEiH8dHYq5TyVtCAg8mtBbZF8gHm/jzVKzLMzOsnzDC+AtC/pKi5bU9QI2gH3cMgdqqWksd06Gz5d9llVHvZ3es1smE9XO8/MaG/yY+rPw6oDBXHKHBzZuEgRsHJ7J4QKBgQDtM5EEw3JsSNwNHQyJXlrMpWNWAUulIJ1ynEmlcp/y8dGaORju8Pj9zZ1N5GrHDy01yaKiETvVVfFuOa0ha8VXxl2I1gPRRnLSujLutTA3k2XWDc50fBbftyKWJL7FnZEqmlvpASyzT9/pzKg+mxQ2yBckAzGXNd+2Kn7A0b9g5QKBgQC50SqcR8cLagklvdZjMwSBhZxQLdJJp/ZPz6rP+ZOLRuplm7nLomEjJfagweH8P7tLBUqWxA9lOlLgB7Y5PIbHtJ7dvTXfuah8XPsk51SR4pB0yxQLOI0y4sMYP+SBphnvoQOk5YL9kS8a1CIU5YK3UeIjuXPfByFNMbhnSDtD5QKBgQCaLVhnhjqLk9gkVeteJoxSVHX64yCuFkMAygE8nwLEQNYCrnY8aTkOj2Vdych11rGGMqeZPCZuBkzFXovgG1LMOM13MdSNZIMh4ZFVg0Px+8bHmXXGVRnxE/JRMVUm0cIXtPlkvDS34IUkTLhW6Sn8aZZQ/BpxtLzShVCaODty2QKBgQCNxr5QR6PjXaBlH5d4UPDbMmq18oBEmtcgxj56ON8Ooakupcb5TIf5HXoz5jrbEHWDJCJpLmbgwcOCE+BBg6ECtxmP7lIwxaN8YCVvXuHz/X5i3qAIf7mcXdClq/pZwbaAU8ZIBqYPersKKVlnwEYgQWXeBB4YyYPrNkiQ5Vfy0QKBgD+vNdKboJQ0nfPMn2gLBcAVROuv+/u5WQku+680xTNYmnb2PCfPML/wMldJZ7TuZ9o0aNtwC9KMj6BiE8Y1ZU7qw2grux3sZZdioNJlWJJzqK06B1WVaMjFFlUAeXH+gl/jTpasHH6l25wyGjAfpjKI1k6oqiKutgg8suO+rmt0";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtGlJZVM4VdtUQ8w2So57YPl0jm07OURZdRwPhKQTc84U2v+jzW9LH5IbH4j3u6oO2gRdPTQlfYQ/WW12I9gJ/8KpufIpzB+EpcYPv70izsxCifVeDPqmSMLfqcct4/J6CX7L2K0hb1uiiFjEICNzFjoaybNnM/pBHyq5bWshzj1SzaOS8/0c2cY86Njix8hpUHdgujLFvQPGRd80R1vuBX/VWr50kRK/Nqei/xhw4fKxWYnO3GCRLKrKOO2uzxBFTbodHQcSrVCw8C4o+Xiyg8CvbK9dh80XHTlUVpiPIi+r8LurUQ+wwC5qPVxXk40BYmsEa7DncxDGav3GJ2cvVwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.shopmail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
